<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta charset="utf-8"/>
    <title></title>

    <meta name="keywords" content="{$Think.CONFIG.keywords}"/>
    <meta name="description" content="{$Think.CONFIG.description}"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"/>

    {include file="public/head" /}

</head>
<body>
<div class="main-container" id="main-container">
    <script type="text/javascript">
        try {
            ace.settings.check('main-container', 'fixed')
        } catch (e) {
        }
    </script>

    <div class="main-container-inner">
        <a class="menu-toggler" id="menu-toggler" href="#">
            <span class="menu-text"></span>
        </a>
        {include file="public/nav"/}


        {include file="public/menu"/}

        <div class="main-content">
            {include file="public/breadcrumb"/}

            <div class="page-content">
                <div class="row">
                    <div class="cf">
                        <!--<a class="btn btn-info" href="{:Url('addFront')}">新增</a>-->
                        <!--数据筛选表单-->
                        <form style="display: inline-block;" class="form-inline fmWorkQuery-hook">
                            <input class="input-small" name="num" placeholder="编号"/>
                            <input class="input" name="name" placeholder="名称"/>
                            <!--<input class="input-small" name="client_name" placeholder="单价"/>-->
                            <button type="button" class="btn btn-info btn-sm" onclick="index.search();">
                                <i class="fa fa-search bigger-110"></i>
                                查找
                            </button>
                            <button type="button" class="btn btn-primary btn-sm" onclick="index.searchNull();">
                                <i class="fa fa-remove bigger-110"></i>
                                清空
                            </button>
                        </form>&nbsp;&nbsp;&nbsp;
                        <!--添加产品表单-->
                        <form style="display: inline-block;" class="form-inline fmGoodAdd-hook">
                            <input class="input-small" name="num" placeholder="编号"/>
                            <input class="input-small" name="name" placeholder="名称"/>
                            <input class="input-small" name="storage" placeholder="数量"/>
                            <input class="input-small" name="price" placeholder="单价"/>
                            <button type="button" class="btn btn-info btn-sm" onclick="index.addGood();">
                                <i class="fa fa-plus bigger-110"></i>
                                添加
                            </button>
                            <button type="reset" class="btn btn-primary btn-sm">
                                <i class="fa fa-reply bigger-110"></i>
                                重置
                            </button>
                        </form>
                    </div>
                    <div class="space-4"></div>
                    <table id="tbWork"></table>
                    <div id="tbWorkPager"></div>

                    <!--入库操作模态窗口-->
                    <div id="modal-fmReStorage" class="modal" tabindex="-1">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                                    <h4 class="blue bigger">
                                        入库 (<span style="color:red;" id="inStorageNum"></span>)
                                    </h4>
                                </div>
                                <div class="modal-body overflow-visible">
                                    <!--入库数量-->
                                    <form class="form-horizontal" id="fmStorage" method="post">
                                        <input type="hidden" name="num" value="">
                                        <div class="form-group">
                                            <label class="col-sm-3 control-label no-padding-right">入库数量</label>
                                            <div class="col-sm-9">
                                                <input type="number" name="storage" class="col-xs-10 col-sm-5" value="">
                                                <span class="help-inline col-xs-12 col-sm-7">
                                <span class="middle"></span>
                              </span>
                                            </div>
                                        </div>
                                    </form>
                                </div>

                                <div class="modal-footer">
                                    <button class="btn btn-sm" data-dismiss="modal">
                                        <i class="icon-remove"></i>
                                        取消
                                    </button>
                                    <button class="btn btn-sm btn-primary" onclick="index.doStorage(this);"
                                            data-loading-text="保存中...">
                                        <i class="icon-ok"></i>
                                        保存
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div><!-- /.row -->
            </div><!-- /.page-content -->
        </div><!-- /.main-content -->

    </div><!-- /.main-container-inner -->

    <a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse">
        <i class="fa fa-angle-double-up fa-only bigger-110"></i>
    </a>
</div>
</body>
{include file="public/footerjs"/}

<script>

    (function () {
        $(function () {
            //加载数据
            var $tbWork = $('#tbWork'), $pager = $('#tbWorkPager');
            $tbWork.jqGrid({
                url: '{:url("good/getGood")}',
                datatype: 'json',
                mttype: 'post',
                colNames: ['编号', '名称', '单价', '库存', '操作'],
                colModel: [
                    {name: 'num', index: 'num', width: '100', align: 'center'},
                    {name: 'name', index: 'name', width: '100', align: 'center'},
                    {name: 'price', index: 'price', width: '100', align: 'center'},
                    {name: 'storage', index: 'storage', width: '100', align: 'center'},
                    {
                        name: 'num', index: 'do', width: '100', align: 'center',
                        formatter: function (value, options, obj) {
                            var reslut = '<a onclick="index.addStorage(\'' + value + '\')" style="cursor:pointer;">入库</a>&nbsp;&nbsp;';
                            reslut += '<a onclick="index.del(\'' + value + '\')" style="cursor:pointer;">删除</a>';
                            return reslut;
                        }
                    },
                ],
                height: 'auto',
                pager: '#tbWorkPager',
                viewrecords: true,
                rowNum: 10,
                rowList: [10, 20, 40],
                autowidth: true
            });
            // 隐藏水平方向滚动条
            $tbWork.closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        });
        index = {
            search: function () {
                var $fmQuery = $('.fmWorkQuery-hook');
                var param = ittaoli.serializeObject($fmQuery);
                $('#tbWork').jqGrid('setGridParam', {
                    postData: {q: JSON.stringify(param)},
                    page: 1
                }).trigger("reloadGrid");
            },
            searchNull: function () {
                $('.fmWorkQuery-hook input').val('');
                var param = {};
                $('#tbWork').jqGrid('setGridParam', {
                    postData: {q: JSON.stringify(param)},
                    page: 1
                }).trigger("reloadGrid");
            },
            addStorage: function (num) {
                $("#modal-fmReStorage").modal("show");
                $("#inStorageNum").text(num);
                $("#fmStorage [name=num]").val(num);
                console.log($("#fmStorage [name=num]").val());
            },
            doStorage: function (obj) {
                var param = ittaoli.serializeObject($('#fmStorage'));
                if (param.num == '') {
                    bootbox.alert('产品编号异常！');
                    return;
                }
                if (param.storage == '') {
                    bootbox.alert('请正确输入正确的数量！');
                    return;
                }
                console.log(JSON.stringify(param));

                $(obj).button('loading');
                $.post('{:url("good/pushGoods")}', {num: param.num, count: param.storage}, function (data) {
                    $('#modal-fmReStorage').modal('hide');
                    $(obj).button('reset');
                    $('#fmStorage')[0].reset();
                    $('#tbWork').jqGrid('setGridParam', {
                        postData: {q: JSON.stringify(ittaoli.serializeObject($('.fmWorkQuery-hook')))}
                    }).trigger("reloadGrid");
                })
            },
            //保存产品零件数据(添加产品模态框的保存事件)
            addGood: function (obj) {
                $(obj).button('loading');
                // business logic...
                var param = ittaoli.serializeObject($('.fmGoodAdd-hook'));
                param.id = 0;
                if (param.num == '') {
                    bootbox.alert('请填写产品编号');
                    return;
                }
                if (param.name == '') {
                    bootbox.alert('请填写产品名称');
                    return;
                }
                if (param.storage == '') {
                    bootbox.alert('请填写产品数量');
                    return;
                }
                if (!ittaoli.regs.int.test(param.storage)) {
                    bootbox.alert('产品数量格式不对哦！');
                    return;
                }
                if (param.price == '') {
                    bootbox.alert('请填写产品单价');
                    return;
                }
                console.log(param);
                $.post('{:url("good/doAdd")}', {param: JSON.stringify(param)}, function (data) {
                    if (data > 0) {
                        $('#tbWork').jqGrid('setGridParam', {
                            postData: {q: JSON.stringify(ittaoli.serializeObject($('.fmWorkQuery-hook')))}
                        }).trigger("reloadGrid");
                        $('.fmGoodAdd-hook')[0].reset();

                        // $('#modal-fmGood').modal('hide');
                    } else {
                        bootbox.alert('保存失败！');
                    }
                    $(obj).button('reset');
                })
            },
            del: function (num) {
                bootbox.confirm("确定要删除？", function (result) {
                    if (result) {
                        $.post("{:url('good/doDel')}", {num: num}, function (data) {
                            if (data > 0) {
                                // bootbox.alert("删除成功");
                                // index.search();
                                $('#tbWork').jqGrid('setGridParam', {
                                    postData: {q: JSON.stringify(ittaoli.serializeObject($('.fmWorkQuery-hook')))}
                                }).trigger("reloadGrid");
                            } else {
                                bootbox.alert("删除失败");
                            }
                        })
                    }
                })
            }
        }
    }());
</script>

{include file="public/foot" /}
